import com.codezen.util.CUtils;

import mx.collections.ArrayCollection;
import mx.utils.ObjectUtil;

private var searchAgain:Boolean = false;

private function findQuery(query:String, len:int = 0):void{
	// hd
	var hd:int = hdOnly?1:0; 
	
	searchAgain = false;
	
	// find
	vkVideoSearch.addEventListener(Event.COMPLETE, onSource);
	vkVideoSearch.findData(query, hd, len);
}
	

private function searchSimpleQuery(query:String):void{
	// clean
	query = query.replace("s:", "");
	
	// change state
	currentState = "Series";
	
	searchAgain = false;
	
	// set window
	series_source_wnd.visible = true;
	series_source_wnd.x = 5;
	series_source_wnd.y = 55;
	series_source_wnd.width = 785;
	series_source_wnd.height = 550;
	source_close_btn.visible = false;
	
	//selectedEpisodeName = kb.seriesInformation.name;
	queryParts = [search_compo.search_input.text];
	
	// hd
	findQuery(query);
}
	

private function onSource(e:Event):void{
	vkVideoSearch.removeEventListener(Event.COMPLETE, onSource);
	// remove busy
	removeBusyState();
	
	// get results
	if(searchAgain){
		epSourceList += vkVideoSearch.resultArray;
	}else{
		epSourceList = vkVideoSearch.resultArray;
	}
	
	// show window
	if(epSourceList.length > 0){
		filterResults();
		
		series_source_wnd.visible = true;
	}else{
		if(queryParts['searched'] != true){
			doSearchAgain();
		}else{
			Alert.show("Ничего не нашлось :(","Ошибка!");
		}
	}
}

private function doSearchAgain():void{
	var query:String;
	var len:int = queryParts['len'];
	if(queryParts['season'] != null){
		query =  queryParts['ename'] + " " + queryParts['season'] +
			" " + queryParts['number'];
	}else{
		query = queryParts['ename'];
	}
	
	queryParts['searched'] = true;
	
	findQuery(query, len);
}

private function filterResults():void{
	if(!filterOn) return;
	// filter trash
	var item:Object;
	var dupes:ArrayCollection = new ArrayCollection();
	var sitem:Object;
	var deleted:Boolean;
	for each(item in epSourceList){
		deleted = false;
		if(
			CUtils.compareStrings(item.title,queryParts['name']) > 50 ||
			CUtils.compareStrings(item.title,queryParts['ename']) > 50 ||
			CUtils.compareStrings(item.title,queryParts['name']+' '+queryParts['ename']) > 50 ||
			CUtils.compareStrings(item.title,queryParts['name']+' '+queryParts['season']+' '+queryParts['number']) > 50 ||
			CUtils.compareStrings(item.title,queryParts['ename']+' '+queryParts['season']+' '+queryParts['number']) > 50 ||
			CUtils.compareStrings(item.title,queryParts['name']+' '+queryParts['ename']+' '+queryParts['season']+' '+queryParts['number']) > 50
		){
			if(dupes.length > 0){
				for each(sitem in dupes){
					if(
						item.url == sitem.url ||
						( CUtils.compareStrings(item.title,sitem.title) > 60 &&
						sitem.len == item.len && sitem.hd == item.hd )
					){
						deleted = true;
						epSourceList.removeItemAt( epSourceList.getItemIndex(item) );
						break;
					}
				}
				if(!deleted) dupes.addItem(item);
			}else{
				dupes.addItem(item);
			}
		}else{
			epSourceList.removeItemAt( epSourceList.getItemIndex(item) );
		}
	}
}








